package com.jxd.ordering.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxd.ordering.dao.MealMapper;
import com.jxd.ordering.model.Meal;
import com.jxd.ordering.service.IMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MealServiceImpl
 * @Description TODO
 * @Author Ankailong
 * @Date 2022/9/28
 * @Version 1.0
 */
@Service
public class MealServiceImpl implements IMealService {
    @Autowired
    private MealMapper mealMapper;

    /**
     * 新增餐品信息
     * @param meal 餐品实体类
     * @return 是否新增成功
     */
    @Override
    public boolean addMeal(Meal meal) {
        return mealMapper.insertMeal(meal);
    }

    /**
     * 修改餐品信息
     * @param meal 餐品实体类
     * @return 是否修改成功
     */
    @Override
    public boolean editMeal(Meal meal) {
        return mealMapper.updateMeal(meal);
    }

    /**
     * 批量删除餐品信息
     * @param mealnos 餐品信息编号数组
     * @return 是否删除成功
     */
    @Override
    public boolean delMeal(int[] mealnos) {
        return mealMapper.deleteMeal(mealnos);
    }

    /**
     * 分页查询全部餐品信息
     * @param queryMap 包含int page, int limit, String mname, double price1, double price2
     * @return 查询结果
     */
    @Override
    public Map<String, Object> getMealByPage(Map<String, String> queryMap) {
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");

        Map<String, Object> map = new HashMap(4);
        if(page != null && limit != null){
            IPage<Meal> pages = new Page(Integer.parseInt(page),Integer.parseInt(limit));
            mealMapper.selectMealByPage(pages, queryMap);

            map.put("count",pages.getTotal());
            map.put("data",pages.getRecords());

            map.put("code",0);
            return map;
        }else{
            map.put("data", (Object)null);
            map.put("code", "500");
            map.put("msg", "参数不符");
            map.put("count", (Object)null);
            return map;
        }
    }

    /**
     * 按餐品编号查询餐品信息
     * @param mealno 餐品编号
     * @return 查询结果
     */
    @Override
    public Meal getMealByMealno(int mealno) {
        return mealMapper.selectMealByMealno(mealno);
    }

    /**
     * 查询全部餐品信息
     * @return 查询结果
     */
    @Override
    public List<Meal> getAllMeal() {
        return mealMapper.selectAllMeal();
    }
}
